from sanic.views import HTTPMethodView
from sanic.response import json
from processing.other_func import ProToken,Sercret_all,Serialization,Random_id,date_format
import json as jn
from processing.Sql_DataFrom import ProJosn


class setMealAdd(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:

            sql = """
                insert into set_meal(user_id,set_meal_money,set_meal_shop,title,details,set_meal_type,set_meal_type_id,sort,image,JSONData,status,create_time ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,now()) 
            """

            db.insert(sql,(
                user_id,
                html_data['set_meal_money'] or '',
                html_data['set_meal_shop'] or '',
                html_data['title'] or '',
                html_data['details'] or '',
                html_data['set_meal_type'] or '',
                html_data['set_meal_type_id'] or '',
                html_data['sort'] or '',
                html_data['image'] or '',
                html_data['JSONData'] or '',
                html_data['status'] or '',
            ))
            return json({'code': 200, 'message': "添加成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/setMeal/add', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)

class setMealList(HTTPMethodView):
    async def get(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json
        pageNum = int(request.args.get('pageNum'))
        pageSize = int(request.args.get('pageSize'))
        try:

            sql = """
               select user_id,set_meal_money,set_meal_shop,title,details,set_meal_type,set_meal_type_id,sort,image,JSONData,status,create_time from set_meal where user_id=%s
           """
            sql += f' ORDER BY create_time DESC '
            # 分页pageNum
            if pageNum == 1:
                sql += f" LIMIT {pageSize} "
            else:
                sql += f" LIMIT {pageNum * pageSize - pageSize},{pageNum * pageSize} "

            data = db.find_all(sql,(user_id))

            data = ProJosn(data,sql).to_dict(orient='records')

            return json({'code': 200, 'message': "请求成功!",'data':data})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/setMeal/list', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)


# 删除会员格式
class setMealDel(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:
            sql = "delete from set_meal where user_id=%s and id=%s"

            db.delete(sql, (user_id,html_data['id']))

            sql1 = "select * from set_meal where  id=%s"
            if db.find_one(sql1, html_data['id']):
                data = {
                    'code': 400,
                    'message': '删除失败!'
                }
            else:
                data = {
                    'code': 200,
                    'message': '删除成功!'
                }
            return json(data)

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/setMeal/delete', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class setMealUpdate(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip
        html_data = request.json

        try:
            sql = """
                   update set_meal set set_meal_money=%s,set_meal_shop=%s,title=%s,details=%s,set_meal_type=%s,set_meal_type_id=%s,sort=%s,image=%s,
                   JSONData=%s,status=%s,create_time=now() where id=%s; 
               """

            db.update(sql,(
                html_data['set_meal_money'] or '',
                html_data['set_meal_shop'] or '',
                html_data['title'] or '',
                html_data['details'] or '',
                html_data['set_meal_type'] or '',
                html_data['set_meal_type_id'] or '',
                html_data['sort'] or '',
                html_data['image'] or '',
                html_data['JSONData'] or '',
                html_data['status'] or '',
                html_data['id']
            ))

            return json({'code': 200, 'message': "修改成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/memberCharge/update', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)